<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="完工单号" prop="designCompletionNumber">
        <el-input
          v-model="queryParams.designCompletionNumber"
          placeholder="请输入完工单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="任务单号" prop="taskNumber">
        <el-input
          v-model="queryParams.taskNumber"
          placeholder="请输入任务单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="客户代码" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入客户代码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部件类型" prop="partType">
        <el-input
          v-model="queryParams.partType"
          placeholder="请输入部件类型"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品规格" prop="productStandard">
        <el-input
          v-model="queryParams.productStandard"
          placeholder="请输入产品规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="dictTicketStatus">
        <el-select
          v-model="queryParams.dictTicketStatus"
          placeholder="请选择状态"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions('technology_ticket_status')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出图类型" prop="dictDrawingType">
        <el-select
          v-model="queryParams.dictDrawingType"
          placeholder="请选择出图类型"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getStrDictOptions('dict_drawing_type')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="creatorName">
        <el-input
          v-model="queryParams.creatorName"
          placeholder="请输入创建人姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="校对人" prop="proofreaderName">
        <el-input
          v-model="queryParams.proofreaderName"
          placeholder="请输入校对人姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="校对时间" prop="proofreaderTime">
        <el-date-picker
          v-model="verifyTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="approverName">
        <el-input
          v-model="queryParams.approverName"
          placeholder="请输入审核人姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="approverTime">
        <el-date-picker
          v-model="auditTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="btnWrap">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['technology:design-complete-ticket:export']"
        >
          数据导出
        </el-button>
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['technology:design-complete-ticket:create']"
        >
          新增
        </el-button>
      </div>
      <div>
        <el-button
          type="primary"
          @click="openTableTitleSet"
        >
          表头设置
        </el-button>
      </div>
    </div>
    <!-- 表单 -->
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      @register="register"
      border
      :tableHeight="tableHeight"
    >
      <!-- 20 已完成 40 待批准 50 待审核 60 待校对 70 编辑中 80 未通过 90 已撤回 -->
      <template #action="{ row }">
        <el-button
          link
          type="primary"
          @click="openForm('update', row.id)"
          v-if="[70, 80].includes(row.dictTicketStatus)"
          v-hasPermi="['technology:design-complete-ticket:update']"
        >
          编辑
        </el-button>
        <el-button
          link
          type="primary"
          v-if="[20,30, 40, 50, 60, 70, 80, 90].includes(row.dictTicketStatus)"
          @click="openForm('view', row.id)"
          v-hasPermi="['technology:design-complete-ticket:view']"
        >
          查看
        </el-button>
      </template>
    </Table>
  </ContentWrap>

  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getTableTitle" />
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
</template>

<script setup lang="ts" name="DesignComplete">
import { getStrDictOptions, getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import * as DesignCompleteTicketApi from '@/api/technology/designCompleteTicket'
import type { CrudSchema } from '@/hooks/web/useCrudSchemas'

const { push } = useRouter()

const queryParams = reactive({
  pageNo: 1,
  designCompletionNumber: null, //设计完工单编号
  taskNumber: null, //任务单编号
  productNumber: null,  //产品编号
  productName: null,
  productStandard: null,  //产品规格
  customerCode: null,
  partType: null,  //部件类型
  dictDrawingType: undefined, //出图类型
  dictTicketStatus: undefined,
  proofreaderName: null, //校对人名称
  proofreaderTime: undefined, //校对时间
  approverName: null,  //审批人名称
  approverTime: undefined,  //审批时间
  creatorName: null,
  createTime: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const verifyTime = ref([])   //校对时间
const auditTime = ref([])   //审核时间
const createTime = ref([])  //创建时间

const tableTitleList = ref<CrudSchema[]>([]) // 表头列表
const code = 'DesignComplete'  //表头名
const tableHeight = ref(0)

// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
const { tableObject, tableMethods } = useTable({
  getListApi: DesignCompleteTicketApi.getDesignCompleteTicketPage, // 分页接口
  defaultParams: queryParams
})

const parentRef = ref()
// 获取table组件
const register = (parent: any) => {
  parentRef.value = parent
}

// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

// 获取表头
const getTableTitle = async () => {
  const res = await DesignCompleteTicketApi.getDesignCompleteTicketPage(queryParams)
  res.labels.push({
    field: 'action',
    label: '操作',
    align: 'left',
    headerAlign: 'left',
    width: 110
  })
  const { allSchemas } = useCrudSchemas(res.labels)
  tableTitleList.value = allSchemas.tableColumns
}

// 打开表头设置
const tableTitleSet = ref()
const openTableTitleSet = () => {
  tableTitleSet.value.open(tableTitleList.value, code)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  queryParams.createTime = createTime.value ? JSON.parse(JSON.stringify(createTime.value)).join(',') : ''
  queryParams.proofreaderTime = verifyTime.value ? JSON.parse(JSON.stringify(verifyTime.value)).join(',') : ''
  queryParams.approverTime = auditTime.value ? JSON.parse(JSON.stringify(auditTime.value)).join(',') : ''
  setSearchParams(queryParams)
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  createTime.value = []
  verifyTime.value = []
  auditTime.value = []
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (type: string, id?: number) => {
  push({
    path:'/technology/technology/drawingProduction/designCompleteTicketDetail',
    query: {
      id,
      type
    }
  })
}

/** 导出按钮操作 */
const exportRef = ref()
const handleExport = async () => {
  exportRef.value.open()
}

// 下载
const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
    fileName: val.name,
    ids: []
  }
  if (val.range === 'all') {
    params.ids = []
  } else {
    params.ids = parentRef.value.selections.map((item) => {
      return item['id']
    })
  }
  const data = await DesignCompleteTicketApi.exportDesignCompleteTicket(params)
  download.excel(data, val.name)
  parentRef.value.clearTableSelect()
}

/** 初始化 **/
onMounted(async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
  tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 115
  await getTableTitle()
  getList()
})
</script>
<style lang="scss" scoped>
.btnWrap {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
</style>
